# -*- coding: utf-8 -*-

import datetime,time

from django.db import models
from django.contrib.auth.models import User
from django.forms import ModelForm
from django.contrib import admin
from django.template import RequestContext, Context, loader,Template


import mptt

""" Signal handling """

import django.dispatch # signals

PreRenderSignal = django.dispatch.Signal(providing_args=["context","view"])


""" Models """

class Menu(models.Model):
    cdate = models.DateTimeField('Dato oprettet',auto_now_add=True,editable=False)
    name = models.CharField('Navn',max_length=50)
    root = models.ForeignKey('MenuItem', null=True, blank=True, related_name='rootmenu',verbose_name='Rod element')

    class Meta:
        get_latest_by='cdate'
        ordering = ('name',)

    def __unicode__(self):
        return self.name


class MenuItem(models.Model):
    cdate = models.DateTimeField('Dato oprettet',auto_now_add=True,editable=False)
    name = models.CharField('Navn',max_length=50)
    index = models.IntegerField('Indeks',default=0)
    menu = models.ForeignKey(Menu, null=True, blank=True, related_name='items',verbose_name='Menu')
    parent = models.ForeignKey('self', null=True, blank=True, related_name='children',verbose_name='Parent')

    class Meta:
        get_latest_by='cdate'
        ordering = ('lft','menu') # admin only honour 1. field

    class EXT:
        fields_list=['name','index','menu','parent']
        fields_edit=['name','index','parent']
        description = u'MenuItem er elementer i menustrukturen. Da der kan være flere menuer, tilhører en MenuItem altid en specifik Menu.'
        object_actions = [('url','View','/testview/?id=$pk$'),
                          ('url','Transform','/trans/$pk$/')]
        rights = {'edit':True, 'add':True, 'delete':True }

    def __unicode__(self):
        return self.name

    def get_indented_name(self,ident='&raquo;'):
        return self.level*ident+self.name
    get_indented_name.short_description = 'name'
    get_indented_name.allow_tags = True

mptt.register(MenuItem, order_insertion_by=['index'])



class Country(models.Model):
    cdate = models.DateTimeField('Dato oprettet',auto_now_add=True,editable=False)
    mdate = models.DateTimeField('Dato oprettet',auto_now=True,editable=False)
    name = models.CharField('Navn',max_length=50)
    code = models.CharField('Kode',max_length=5)
    activedate = models.DateField('Aktiveringsdato',blank=True,null=True)
    menu = models.ForeignKey(Menu, null=True, blank=True, related_name='countries',verbose_name='Menu')

    class Meta:
        get_latest_by='cdate'
        ordering = ('name',)

    def __unicode__(self):
        return self.name
